package dao;

import java.util.ArrayList;

import bean.ConnectionMessage;
import bean.CreateCombinaisonMessage;
import bean.CreateUserMessage;
import bean.DeleteUserMessage;
import bean.ErrorBean;
import bean.LoadCombinaisonListMessage;
import bean.LoadUserListMessage;
import bean.Message;
import bean.PlayMessage;
import bean.UpdateUserMessage;

public class RedirectionDao {
	public Message connection(Message mIn){
		ConnectionMessage cmIn = (ConnectionMessage)mIn;
		ConnectionMessage cmOut = new ConnectionMessage();
		DaoUser daoUser = new DaoUser();
		cmOut.setUser(daoUser.connection(cmIn.getUser()));
		return cmOut;
	}
	public Message createUser(Message mIn){
		CreateUserMessage cmIn = (CreateUserMessage)mIn;
		CreateUserMessage cmOut = new CreateUserMessage();
		DaoUser daoUser = new DaoUser();
		cmOut.setUser(daoUser.CreateUser(cmIn.getUser()));
		return cmOut;
	}
	public Message updateUser(Message mIn) {
		UpdateUserMessage cmIn = (UpdateUserMessage)mIn;
		UpdateUserMessage cmOut = new UpdateUserMessage();
		DaoUser daoUser = new DaoUser();
		cmOut.setUser(daoUser.updateUser(cmIn.getUser()));
		return cmOut;
	}
	public Message getAllUsers(Message mIn){
		LoadUserListMessage ulmOut = new LoadUserListMessage();
		DaoUser daoUser = new DaoUser();
		ulmOut.setListUser(daoUser.getAllUsers());
		return ulmOut;
	}
	public Message deleteUser(Message mIn){
		DeleteUserMessage dumIn = (DeleteUserMessage)mIn;
		DeleteUserMessage dumOut = new DeleteUserMessage();
		DaoUser daoUser = new DaoUser();
		daoUser.deleteUser(dumIn.getUser());
		return dumOut;
	}
	public Message getCombinaisonsList(Message mIn){
		LoadCombinaisonListMessage lclIn = (LoadCombinaisonListMessage)mIn;
		LoadCombinaisonListMessage lclOut = new LoadCombinaisonListMessage();
		DaoCombinaison daoCombi = new DaoCombinaison();
		if(lclIn.getFilter().equals("all")){
			lclOut.setListCombi(daoCombi.getAllCombinaisons());
		}else if(lclIn.getFilter().equals("notFind")){
			lclOut.setListCombi(daoCombi.getNotFindCombinaisons(lclIn.getIdUser()));
		}else if(lclIn.getFilter().equals("owned")){
			lclOut.setListCombi(daoCombi.getOwnedCombinaisons(lclIn.getIdUser()));
		}else if(lclIn.getFilter().equals("find")){
			lclOut.setListCombi(daoCombi.getFindCombinaisons(lclIn.getIdUser()));
		}else
		{
			System.out.println("error with message filter in getCOmbinaisonList : "+lclIn.getFilter());
		}
		return lclOut;
	}
	
	public Message createCombinaison(Message mIn){
		CreateCombinaisonMessage ccmIn = (CreateCombinaisonMessage) mIn;
		CreateCombinaisonMessage ccmOut = new CreateCombinaisonMessage();
		ccmOut.setErrors(new ArrayList<ErrorBean>());
		DaoCombinaison daoCombi = new DaoCombinaison();

		if (daoCombi.checkExistsName(ccmIn.getCombi())){
			ErrorBean e = new ErrorBean();
			e.setLevel(1);
			e.setType("combinaisonNameExists");
			ccmOut.getErrors().add(e);
			System.out.println("name");
		}else if(daoCombi.checkExistsCombinaison(ccmIn.getCombi())){
			ErrorBean e = new ErrorBean();
			e.setLevel(1);
			e.setType("combinaisonNameExists");
			e.setType("combinaisonExists");
			ccmOut.getErrors().add(e);
			
		}else{
		ccmOut.setCombi(daoCombi.create(ccmIn.getCombi()));
		}
		return ccmOut;
	}
	
	public Message saveWin(Message mIn){
		PlayMessage pmIn = (PlayMessage)mIn;
		DaoUserHasCombinaisons dao = new DaoUserHasCombinaisons();
		dao.create(pmIn.getIdUser(), pmIn.getCombi().getIdCombinaison());
		return mIn;
	}
}
